﻿<?php
global $config;
$act = "";
$newsId = -1;
$page = 1;
if (isset($_GET['act']))
	$act = $_GET['act'];
	
if (isset($_GET['id']))
	$newsId = $_GET['id'];

if (isset($_GET['page']))
	$page = $_GET['page'];

switch ($act)
{
	case "list":
		ShowListView($page, $config['news_per_page']);
		break;
	case "view":
		ShowNews($newsId);
		break;
	default:
		ShowListView($page, $config['news_per_page']);
		break;
}

function ShowListView($page = 1, $limit = 10)
{
	global $db;
	$start = $limit * ($page - 1);
    $sql = "SELECT * FROM news ORDER BY id DESC LIMIT $start, $limit";
	$db->query($sql);
	$data = $db->get_data();
    $news_url = "index.php?mod=news&act=view&id=";
	echo "\n<h3 class='cat-title'>Tin Tức</h3>";
	if (!empty($data))
	{
        foreach ($data as $news) 
        {
			echo "\n<div class='news-box-content'>";
            echo "\n<a href='{$news_url}{$news['id']}' title='{$news['title']}'><img class='news-img' src='".getThumbnail($news['thumbnail'])."' alt='{$news['title']}' /></a>";
            echo "\n<h4><a href='{$news_url}{$news['id']}' title='{$news['title']}'>{$news['title']}</a></h4>";
            echo "\n<p class='news-summary'>{$news['summary']}</p>";
            echo "\n<a href='{$news_url}{$news['id']}' class='read-more' title='Xem tiếp'>Xem tiếp</a>";
			echo "\n<div class='clear'></div>";
            echo "\n</div><!-- end .news-box-content -->";
        }
		ViewPages($limit);
    }
}

function ShowNews($newsId) {
    global $db;
    $sql = "SELECT * FROM news WHERE id = $newsId";
    $db->query($sql);
    $news = $db->fetch_array();
    
    if (!empty($newsId)) {
        $db->query("update news set viewed = viewed + 1 where id = $newsId");
        NewsDetail($news);
    }
}

function NewsDetail($data) {
    echo "\n<div class='news-detail'>";         
    echo "\n<h4 class='news-title'>{$data['title']}</h4>";
    echo "\n<div class='news-metadata'>Đăng lúc: {$data['created']} - Đã xem: {$data['viewed']}</div>";                   
    echo "\n<div class='news-detail-summary'>{$data['summary']}</div>";                   
    echo "\n<div class='news-content'>{$data['content']}</div>";
    echo "\n</div><!-- end .news-detail -->";
}

?>